package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/4 15:13
 */
class P1 : TestDsl {

    private fun twoSum(nums: IntArray, target: Int): IntArray {
        val cache = HashMap<Int, Int>()
        for (i in nums.indices) {
            cache[nums[i]] = i
        }

        for (i in nums.indices) {
            val hold = nums[i]
            val lack = target - hold
            if (cache.containsKey(lack)) {
                val lackValueIdx = cache[lack]!!
                if (lackValueIdx != i) {
                    return intArrayOf(i, lackValueIdx)
                }
            }
        }

        return intArrayOf()
    }

    @Test
    fun test0() {
        twoSum(intArrayOf(2, 7, 11, 15), 9).log()
    }

}
